package test;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class test {
    void testPost(String urlStr) {
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
//            con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");

            OutputStreamWriter out = new OutputStreamWriter(con
                .getOutputStream());
            String xmlInfo = getXmlInfo();
            System.out.println("urlStr=" + urlStr);
            System.out.println("xmlInfo=" + xmlInfo);
            out.write(new String(xmlInfo.getBytes("ISO-8859-1")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            for (line = br.readLine(); line != null; line = br.readLine()) {
                System.out.println(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void testPost11(String urlStr) {
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
//            con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");

            OutputStreamWriter out = new OutputStreamWriter(con
                .getOutputStream());
            String xmlInfo = getXmlInfo();
            System.out.println("urlStr=" + urlStr);
            System.out.println("xmlInfo=" + xmlInfo);
            out.write(new String(xmlInfo.getBytes("ISO-8859-1")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            for (line = br.readLine(); line != null; line = br.readLine()) {
                System.out.println(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url
     *            发送请求的 URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public String sendPost(String url) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(getXmlInfo());
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    private String getXmlInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" ?><service><head><tran_id>YSHD.OAUTH.TOKEN</tran_id><channel_id>YSZTC.SWKD.JKFW</channel_id><tran_seq>455053a03f4d41e296d0ac8f6119c807</tran_seq><tran_date>20200417</tran_date><tran_time>145000070</tran_time></head><body>R7/5CQPiBdXbtDAWfAkzM1D3jvWUQuBsuN29UWKh/+NlI2ujk3lPjCp5+qZjjEsfvYPTHLp/YCwn ");
        sb.append("TJssu0YZ538NhwIXL1v4xX88PiZBqPntTS2/0DEGbbZqj27eTxbjkoy8OoHF93zKGzsRzI0x4Gyj ");
        sb.append("oRFjOyDpKNHv+XLNJ8+GTtboSQv2L+F61eEOJc5E2W+iSZh3krGQpu3ZvKcSaUiHolcs2U5Lyo62 ");
        sb.append("Vboqu4ZCqaziotT/9KVzxiVtMsvaU2k2ZqOpdIMS6Ba1mP8C9FS5oFaQDAlLCM6dOz1jSjLm0/4j ");
        sb.append("Na5RiyaoGBDyQHBmy7JU7ER8AGMsrD7VrVCMqEVU2mOSrye7lUBJMHfpbG29moPuYw/6QmOy91+p ");
        sb.append("xAlij6DE40V6mdjdMcDFfz65Aw9YU5qyQLUqKYmKDwRQeHNUSimingauSHhBbayrAuNBcxihfsc6 ");
        sb.append("3gd0K/syKy/LwQ==</body></service>");
        return sb.toString();
    }

    public static void main(String[] args) {
        String url = "http://tsrj.henan.chinatax.gov.cn/yshdjk/yshd/jkfw/sendXML.do";
        new test().testPost(url);
    }
}
